import Vue from 'vue'
import VueRouter from 'vue-router'
import Manage from '../views/Manage.vue'
import el from "element-ui/src/locale/lang/el";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'manage',
        component: Manage,
        redirect: "/home",//重定向到home页面
        children: [
            {
                path: 'home', name: 'Home', component: () => import('../views/Home.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'user', name: 'User', component: () => import('../views/User.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'message', name: '消息', component: () => import('../views/Message.vue')
            },
            {
                path: 'userLoginLog', name: '用户登录日志', component: () => import('../views/UserLoginLog.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'userDistribution',
                name: '用户分布',
                component: () => import('../views/UserDistribution.vue'),
                meta: {
                    keepAlive: true
                }
            },
            {
                path: 'consumerTaste', name: '用户爱好', component: () => import('../views/ConsumerTaste.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'realTime', name: '实时数据', component: () => import('../views/RealTime.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'realTime2', name: '实时数据2', component: () => import('../views/RealTime2.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'person', name: '个人信息', component: () => import('../views/Person.vue'), meta: {
                    keepAlive: true
                }
            },
            {
                path: 'userFeedback', name: '用户反馈', component: () => import('../views/UserFeedback.vue'), meta: {
                    keepAlive: true
                }
            }
        ]
    },
    {
        path: '/about',
        name: 'about',
        component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },

]


const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

// to:即将进入的路由对象
// from:当前导航即将离开额路由
// next:进行管道中的一个钩子，如果执行完了，那么导航的状态就是 confirmed，否则为false，终止导航

router.beforeEach((to, from, next) => {
    console.log(this)
    if (to.path === '/login') {
        //如果去登录页面，就放行
        next();
    } else if (JSON.parse(localStorage.getItem("user")) != null) {
        console.log(JSON.parse(localStorage.getItem("user")))
        //如果登录了，就放行
        next();
    } else {
        //如果没有登录，那么就跳转带登录页面
        next("/login");
    }
})

export default router
